﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using ConfigDic = System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>>;
using Common;
using System;

namespace AI.FSM
{
	/// <summary>
    /// AI配置文件读取器
    /// </summary>
	public class AIConfigurationReader {
        private static string mainKey;

        //private static ConfigDic map;
        ////读取配置文件
        //private static string GetConfigFile()//string path需要更改
        //{
        //    //构建路径
        //    string configPath = Application.streamingAssetsPath + "/AI_01.txt";
        //    if (Application.platform != RuntimePlatform.Android)
        //    {
        //        configPath = "file://" + configPath;//如果是安卓平台则是"jar:file://"
        //    }
        //    //加载文件
        //    WWW www = new WWW(configPath);//又可以访问外网WWW www = new WWW("http://");，又可以访问本地
        //    while (true)
        //    {
        //        //是否加载完成
        //        if (www.isDone)
        //        {
        //            return www.text;
        //        }
        //    }
        //}
        //形成数据结构：状态编号     条件编号，状态编号
        //Dictionary<string, Dictionary<string,string>>
        //public static ConfigDic BuildMap(string aiPath)
        //{

        //    string strConfig = ConfigruationReader.GetConfigFile(aiPath);
        //    ConfigDic map = new ConfigDic();
        //    //字符串读取器：逐行读取
        //    StringReader reader = new StringReader(strConfig);
        //    string line = null;
        //    string mainKey = null;
        //    while ((line = reader.ReadLine()) != null)
        //    {
        //        //去除空白
        //        line = line.Trim();
        //        //跳过空行
        //        if (string.IsNullOrEmpty(line))//等价于if (line == null || line == "")
        //        {
        //            continue;
        //        }
        //        //[Idle]
        //        //NoHealth>Dead
        //        if (line.StartsWith("["))
        //        {
        //            //大字典的键
        //            mainKey = line.Substring(1, line.Length - 2);//其他方法mainKey = line.Replace("[", "").Replace("]","");
        //            map.Add(mainKey, new Dictionary<string, string>());
        //        }
        //        else
        //        {
        //            //小字典的记录
        //            string[] configValue = line.Split('>');
        //            map[mainKey].Add(configValue[0], configValue[1]);
        //        }
        //    }
        //    //如果读取到内容，则不为空
        //    return map;
        //}

        public static ConfigDic Load(string aiPath)
        {
            string configFile = ConfigruationReader.GetConfigFile(aiPath);
            ConfigDic map = new ConfigDic();
            ConfigruationReader.BuildMap(configFile, map, BuildMapHandler);
            return map;
        }

        private static void BuildMapHandler(ConfigDic map, string line)
        {
            line = line.Trim();
            //跳过空行
            if (string.IsNullOrEmpty(line))//等价于if (line == null || line == "")
            {
                return;
            }
            //[Idle]
            //NoHealth>Dead
            if (line.StartsWith("["))
            {
                //大字典的键
                mainKey = line.Substring(1, line.Length - 2);//其他方法mainKey = line.Replace("[", "").Replace("]","");
                map.Add(mainKey, new Dictionary<string, string>());
            }
            else
            {
                //小字典的记录
                string[] configValue = line.Split('>');
                map[mainKey].Add(configValue[0], configValue[1]);
            }
        }
    }
}

